package com.lyhlmj.oop_client.Utils;

import java.awt.*;

//工具类，设置窗口的布局
public class CenterUtils {

    //    设置窗口在屏幕中央
    public static void centerInScreen(Component component) {
//        使窗口显示在屏幕的正中间
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int fitX = (screenSize.width - component.getWidth()) / 2;
        int fitY = (screenSize.height - component.getHeight()) / 2;

        component.setLocation(fitX, fitY);
    }

    //    设置窗口显示在父类窗口中心
    public static Rectangle centerInOwner(Window owner, int width, int height) {
//        设置对话框显示在原窗口的中央位置
        Rectangle ownerRect = owner.getBounds();

        int fitX = ownerRect.x + (ownerRect.width - width) / 2;
        int fitY = ownerRect.y + (ownerRect.height - height) / 2;
        return new Rectangle(fitX, fitY, width, height);
    }
}
